package com.test.test2.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.test.test2.dao.pojo.Employee;
import com.test.test2.service.EmployeeService;
import com.test.test2.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;

    @PostMapping("/addEmployee")
    public Result addEmp(@RequestBody Employee employee){
        employeeService.addEmp(employee);
        Result.success(employee).setMsg("插入成功！");
        return Result.success(employee);
    }

    @PostMapping("/delEmployee/{id}")
    public Result delEmp(@PathVariable("id") long id){
        employeeService.delEmp(id);
        Employee empById = employeeService.findById(id);
        if (empById!=null){
            return Result.fail(400,"删除失败");
        }
        Result.success(empById).setMsg("删除成功！");
        return Result.success(empById);
    }

    @PostMapping("/updateEmp")
    public Result updateEmp(@RequestBody Employee employee){
        Employee orinEmp = employeeService.findById(employee.getId());
        employeeService.updateEmpById(employee);
        Employee empById = employeeService.findById(employee.getId());
        if (orinEmp!=empById){
            return Result.fail(400,"修改失败");
        }
        Result.success(employee).setMsg("修改成功！");
        return Result.success(employee);
    }

    @PostMapping("/findById")
    public Result findById(long id){
        Employee empById = employeeService.findById(id);
        Result.success(empById).setMsg("查询成功！");
        return Result.success(empById);
    }

    @PostMapping("/findAll")
    public Result findAll(){
        List<Employee> findAll = employeeService.findAll();
        Result.success(findAll).setMsg("查询成功！");
        return Result.success(findAll);
    }

    @GetMapping("/searchDpEmployees/{currentPage}/{pageSize}")
    public Result searchDpEmployees(@PathVariable("currentPage") long currentPage,@PathVariable("pageSize")long pageSize){
        Page<Employee> employeePage = employeeService.searchDpEmp(currentPage, pageSize);
        Result.success(employeePage).setMsg("分页查询成功！");
        return Result.success(employeePage);
    }
}
